/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txcms.admin.action;

import cn.txdnet.txcms.domain.Events;
import cn.txdnet.txcms.query.EventsQuery;
import cn.txdnet.txcms.service.EventsService;
import cn.txdnet.txweb.annotation.methods.PostMethod;
import cn.txdnet.txweb.model.ModelUtil;
import cn.txdnet.txweb.model.ModelView;
import cn.txdnet.txweb.utils.BeanUtil;
import cn.txdnet.txweb.utils.Message;
import cn.txdnet.txweb.utils.WebUtil;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author txdnet
 */
public class EventsAction {

    /**
     * 保存活动信息
     *
     * @param req
     * @return
     * @throws Exception
     */
    @PostMethod
    public ModelView save(HttpServletRequest req) throws Exception {
        Events event = BeanUtil.parseRequestToBean(Events.class, req);
        if (!event.validate()) {
            return ModelUtil.getTextModel(Message.MSG_FIELD_ERR);
        }
        if (EventsService.make(event)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更新活动信息
     *
     * @param req
     * @return
     * @throws Exception
     */
    @PostMethod
    public ModelView update(HttpServletRequest req) throws Exception {
        Events event = BeanUtil.parseRequestToBean(Events.class, req);
        if (!event.validate()) {
            return ModelUtil.getTextModel(Message.MSG_FIELD_ERR);
        }
        if (EventsService.update(event)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量删除活动信息
     *
     * @param req
     * @return
     */
    public ModelView delete_batch(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        if (ids == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int del_num = EventsService.deleteBatch(ids);
        if (del_num > 0) {
            return ModelUtil.getTextModel(del_num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }

    }

    /**
     * 批量更新发布状态
     *
     * @param req
     * @return
     */
    public ModelView update_pubstate(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        byte pubstate = WebUtil.getByte(req, "pubstate", -1);
        if (ids == null || pubstate < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = EventsService.updatePubstateBatch(ids, pubstate);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新信息频道和类目信息
     *
     * @param req
     * @return
     */
    public ModelView update_channel_catelog(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        int cid = WebUtil.getInt(req, "cid", -1);
        String ctp = WebUtil.getNoNull(req, "ctp", null);
        byte channeltype = WebUtil.getByte(req, "channeltype", -1);
        if (ids == null || cid < 0 || ctp == null || channeltype < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = EventsService.updateChannelAndCatalogBatch(ids, channeltype, cid, ctp);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新发布时间
     *
     * @param req
     * @return
     */
    public ModelView update_pubtime(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        long pubtime = WebUtil.getLong(req, "pubtime", -1);
        if (ids == null || pubtime < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = EventsService.updatePubTimeBatch(ids, pubtime);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新结束时间
     *
     * @param req
     * @return
     */
    public ModelView update_endtime(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        long endtime = WebUtil.getLong(req, "endtime", 0);
        if (ids == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = EventsService.updateEndTimeBatch(ids, endtime);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更具条件查询记录
     *
     * @param req
     * @return
     * @throws Exception
     */
    public ModelView list_factor(HttpServletRequest req) throws Exception {
        EventsQuery query = BeanUtil.parseRequestToBean(EventsQuery.class, req);
        return ModelUtil.getJsonModel(EventsService.queryByFactor(query));
    }

    /**
     * 初始化信息编辑表单界面
     *
     * @param req
     * @return
     */
    public ModelView query_event_edit(HttpServletRequest req) {
        int id = WebUtil.getInt(req, "id", -1);
        if (id < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        Events event = EventsService.queryEventsForEdit(id);
        if (event == null) {
            return ModelUtil.getTextModel(Message.MSG_VALUE_NULL);
        }
        return ModelUtil.getJsonModel(event);
    }
}
